<template>
	<view class="content">
		<view v-if='orders && orders.length > 0' style='padding-top:30px'>
			<view v-for='item in orders'>
				<view class='group'>
					<view style='padding:10px 0px;'>
						<text style='color:#303133;font-weight: bold;'>单号：{{item.no.slice(-8)}}</text>
						<text style='color:#606266;font-weight: bold;float:right'>{{orderStatus[item.status]}}</text>
					</view>
					<u-line></u-line>
					<view>
						<view v-for='spec in item.items'>
							<view>
								<view style='color:#303133;font-weight: bold;line-height: 40px;'>
									<text style='color:#f9ae3d'>{{spec.__metadata.specification.__metadata.product.__toString}}({{spec.__metadata.specification.__metadata.name}})</text>
									<text style='color:#909399;float:right'>￥{{spec.__metadata.unitPrice}} X {{spec.__metadata.quantity}}</text>
								</view>
							</view>
							<u-line></u-line>
						</view>
						<view>
							<view style='color:#303133;font-weight: bold;line-height: 40px;'>
								<text style='color:#f9ae3d'>合计：</text>
								<text style='color:#909399;float:right'>￥{{item.price}}</text>
							</view>
							<u-line></u-line>
						</view>
					</view>

					<view style='padding-top:5px'>
						<view v-if='item.staff'>
							<view>
								<text class='title'>{{item.staff.__toString}}</text>
								<text
									class='name'>￥{{(parseFloat(item.price)+parseFloat(item.paidCounter)).toFixed(2)}}</text>
							</view>
							<u-line></u-line>
						</view>
						<view>
							<text class='title'>创建时间</text>
							<text class='name'>
								{{$u.timeFormat(new Date(item.createdTime), 'yyyy-mm-dd hh:MM:ss')}}
							</text>
						</view>
						<u-line></u-line>
					</view>
					<view style='padding:10px;'>
						<view v-if='item.status == 1'>
							<u-button type='success' text='支付订单' :plain='true' shape='circle' @click='toUrl(`/pages/payment/pay-code?invoice=${item.invoice.id}`)'></u-button>
						</view>
						<view v-if='item.status == 2'>
							<u-button type='primary' text='订单核销' :plain='true' shape='circle' @click='toUrl(`/pages/orders/verify-code?id=${item.id}`)'></u-button>
						</view>
					</view>
				</view>
			</view>
			<u-loadmore :status="status" />
		</view>
		<view v-else style='padding-top:30%'>
			<u-empty mode="order" text='无订单记录'></u-empty>
		</view>
	</view>
</template>

<script>
	import uQRCode from '@/uni_modules/Sansnn-uQRCode/components/uqrcode/uqrcode.vue'
	export default {
		data() {
			return {
				orders: [],
				orderStatus: {
					'-2': '支付失败',
					'-1': '已取消',
					0: '等待支付',
					1: '待支付',
					2: '待核销',
					3: '已核销',
					4: '已结算',
					5: '已完成'
				},
				status: 'loadmore',
				limit: 10,
				page: 1
			}
		},
		onShow() {
			let token = uni.getStorageSync('api_token')
			if(token != ''){
				this.orders = []
				this.page = 1
				this.status = 'loadmore'
				this.getOrder()
			}else{
				uni.showModal({
					title: '',
					content: '请先登录',
					showCancel: false,
					success: () => {
						uni.navigateTo({
							url: '/pages/index/login'
						})
					}
				})
			}
		},
		onLoad() {
			
		},
		onReachBottom() {
			if (this.status == 'loadmore') {
				this.page++
				this.getOrder()
			}
		},
		methods: {
			getOrder() {
				uni.showLoading()
				this.request.get(`api/orders`, {
					'@filter': `entity.getStatus() != 0`,
					limit: this.limit,
					page: this.page,
					'@order': 'entity.createdTime|DESC',
					'@expands': '["entity.items.specification.product","entity.invoice"]'
				}).then(res => {
					if (res.data.length < this.limit) {
						this.status = 'nomore'
					}
					for (let i of res.data) {
						this.orders.push(i)
					}
					console.log('orders', this.orders)
					uni.hideLoading()
				})
			},
			toUrl(page) {
				this.loading = true
				uni.navigateTo({
					url: page
				})
			}
		}
	}
</script>

<style>
	.content {
		background-color: #f3f3f3;
		min-height: 100vh;
	}

	.group {
		margin: 10px;
		background-color: #fff;
		border-radius: 10px;
		padding: 10px;
	}

	.title {
		color: #909399;
		font-size: 14px;
		line-height: 32px;
	}

	.name {
		font-size: 14px;
		float: right;
		padding-right: 10px;
		line-height: 32px;
		color: #303133;
	}

	.date-btn {
		color: #f3f3f3;
		margin: 5px;
		background-color: #434152;
		border-radius: 5px;
		padding: 10px;
		text-align: center;
	}
</style>